package com.emath.objects;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONException;
import android.app.Activity;
import android.app.PendingIntent;

import com.lnl.R;
import com.emath.activities.GamePlay;
import com.emath.shared.BigRational;
import com.emath.shared.RestClient;

public class Game {
	public static int stack;
	public static String GameID;
	public static boolean Createdgame;
	public static boolean firstturn = true;
	public static int mydiceroll = 0;
	public static int opponentdiceroll = 0;
	public static int stackindex;
	public static boolean exit = false;
	public static PendingIntent intent;
	public static int opponentnumcharacters() {
		int count = 0;
		for (int i = 0; i < OpponentPlayer.stack1.size(); i++)
			if (OpponentPlayer.stack1.get(i).cardType.equals("Character")
					&& !OpponentPlayer.stack1.get(i).tapped)
				count++;

		for (int i = 0; i < OpponentPlayer.stack2.size(); i++)
			if (OpponentPlayer.stack2.get(i).cardType.equals("Character")
					&& !OpponentPlayer.stack2.get(i).tapped)
				count++;
		for (int i = 0; i < OpponentPlayer.stack3.size(); i++)
			if (OpponentPlayer.stack3.get(i).cardType.equals("Character")
					&& !OpponentPlayer.stack3.get(i).tapped)
				count++;
		for (int i = 0; i < OpponentPlayer.stack4.size(); i++)
			if (OpponentPlayer.stack4.get(i).cardType.equals("Character")
					&& !OpponentPlayer.stack4.get(i).tapped)
				count++;
		for (int i = 0; i < OpponentPlayer.stack5.size(); i++)
			if (OpponentPlayer.stack5.get(i).cardType.equals("Character")
					&& !OpponentPlayer.stack5.get(i).tapped)
				count++;
		return count;

	}
	public static int numcharacters() {
		int count = 0;
		for (int i = 0; i < Player.stack1.size(); i++)
			if (Player.stack1.get(i).cardType.equals("Character")
					&& !Player.stack1.get(i).tapped)
				count++;

		for (int i = 0; i < Player.stack2.size(); i++)
			if (Player.stack2.get(i).cardType.equals("Character")
					&& !Player.stack2.get(i).tapped)
				count++;
		for (int i = 0; i < Player.stack3.size(); i++)
			if (Player.stack3.get(i).cardType.equals("Character")
					&& !Player.stack3.get(i).tapped)
				count++;
		for (int i = 0; i < Player.stack4.size(); i++)
			if (Player.stack4.get(i).cardType.equals("Character")
					&& !Player.stack4.get(i).tapped)
				count++;
		for (int i = 0; i < Player.stack5.size(); i++)
			if (Player.stack5.get(i).cardType.equals("Character")
					&& !Player.stack5.get(i).tapped)
				count++;
		return count;
	}

	public static int numresources() {
		int count = 0;

		for (int i = 0; i < Player.stack1.size(); i++)
			if (!Player.stack1.get(i).tapped
					&& Player.stack1.get(i).cardType.equals("Resource")) {
				count++;
			}
		for (int i = 0; i < Player.stack2.size(); i++)
			if (!Player.stack2.get(i).tapped
					&& Player.stack2.get(i).cardType.equals("Resource")) {
				count++;
			}
		for (int i = 0; i < Player.stack3.size(); i++)
			if (!Player.stack3.get(i).tapped
					&& Player.stack3.get(i).cardType.equals("Resource")) {
				count++;
			}
		for (int i = 0; i < Player.stack4.size(); i++)
			if (!Player.stack4.get(i).tapped
					&& Player.stack4.get(i).cardType.equals("Resource")) {
				count++;
			}
		for (int i = 0; i < Player.stack5.size(); i++)
			if (!Player.stack5.get(i).tapped
					&& Player.stack5.get(i).cardType.equals("Resource")) {
				count++;
			}
		return count;
	}

	public static void setmystacks() {
		RestClient Connect = new RestClient();

		String setstack = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
				+ Game.getGameID();
		boolean oneempty = true;
		String setstacks = "";
		for (int i = 0; i < Player.stack1.size(); i++)
			if (Player.stack1.get(i).cardID != 0) {
				oneempty = false;
				if (Player.stack1.get(i).tapped)
					setstacks = setstacks + Player.stack1.get(i).cardID + "t";
				if (!Player.stack1.get(i).tapped)
					setstacks = setstacks + Player.stack1.get(i).cardID;

				setstacks = setstacks + "lvl" + Player.stack1.get(i).getLevel();

				if (i != Player.stack1.size() - 1)
					setstacks = setstacks + ",";

			}
		if (!oneempty)
			setstack = setstack + "&" + Player.getPlayer() + "stack1="
					+ setstacks;
		else
			setstack = setstack + "&" + Player.getPlayer() + "stack1=null";

		boolean twoempty = true;
		setstacks = "";
		for (int i = 0; i < Player.stack2.size(); i++)
			if (Player.stack2.get(i).cardID != 0) {
				twoempty = false;
				if (Player.stack2.get(i).tapped)
					setstacks = setstacks + Player.stack2.get(i).cardID + "t";
				if (!Player.stack2.get(i).tapped)
					setstacks = setstacks + Player.stack2.get(i).cardID;

				setstacks = setstacks + "lvl" + Player.stack2.get(i).getLevel();

				if (i != Player.stack2.size() - 1)
					setstacks = setstacks + ",";
			}
		if (!twoempty)
			setstack = setstack + "&" + Player.getPlayer() + "stack2="
					+ setstacks;
		else
			setstack = setstack + "&" + Player.getPlayer() + "stack2=null";

		setstacks = "";
		boolean threeempty = true;
		for (int i = 0; i < Player.stack3.size(); i++)
			if (Player.stack3.get(i).cardID != 0) {
				threeempty = false;
				if (Player.stack3.get(i).tapped)
					setstacks = setstacks + Player.stack3.get(i).cardID + "t";
				if (!Player.stack3.get(i).tapped)
					setstacks = setstacks + Player.stack3.get(i).cardID;

				setstacks = setstacks + "lvl" + Player.stack3.get(i).getLevel();

				if (i != Player.stack3.size() - 1)
					setstacks = setstacks + ",";

			}
		if (!threeempty)
			setstack = setstack + "&" + Player.getPlayer() + "stack3="
					+ setstacks;
		else
			setstack = setstack + "&" + Player.getPlayer() + "stack3=null";

		boolean fourempty = true;
		setstacks = "";
		for (int i = 0; i < Player.stack4.size(); i++)
			if (Player.stack4.get(i).cardID != 0) {
				fourempty = false;
				if (Player.stack4.get(i).tapped)
					setstacks = setstacks + Player.stack4.get(i).cardID + "t";
				if (!Player.stack4.get(i).tapped)
					setstacks = setstacks + Player.stack4.get(i).cardID;

				setstacks = setstacks + "lvl" + Player.stack4.get(i).getLevel();

				if (i != Player.stack4.size() - 1)
					setstacks = setstacks + ",";

			}
		if (!fourempty)
			setstack = setstack + "&" + Player.getPlayer() + "stack4="
					+ setstacks;
		else
			setstack = setstack + "&" + Player.getPlayer() + "stack4=null";

		setstacks = "";
		boolean fiveempty = true;
		for (int i = 0; i < Player.stack5.size(); i++)
			if (Player.stack5.get(i).cardID != 0) {
				fiveempty = false;
				if (Player.stack5.get(i).tapped)
					setstacks = setstacks + Player.stack5.get(i).cardID + "t";
				if (!Player.stack5.get(i).tapped)
					setstacks = setstacks + Player.stack5.get(i).cardID;

				setstacks = setstacks + "lvl" + Player.stack5.get(i).getLevel();

				if (i != Player.stack5.size() - 1)
					setstacks = setstacks + ",";

			}
		if (!fiveempty)
			setstack = setstack + "&" + Player.getPlayer() + "stack5="
					+ setstacks;
		else
			setstack = setstack + "&" + Player.getPlayer() + "stack5=null";

		Connect.connect(setstack);
		Connect.clear();

	}
	public static int endgame() {
		if (Player.health.doubleValue() <= 0)
			return 1;
		if (OpponentPlayer.health.doubleValue() <= 0)
			return 2;

		return 0;

	}

	public static int countallresources() {
		int count = 0;
		for (int i = 0; i < Player.stack1.size(); i++)
			if (Player.stack1.get(i).cardType.equals("Resource")) {
				count++;
			}
		for (int i = 0; i < Player.stack2.size(); i++)
			if (Player.stack2.get(i).cardType.equals("Resource")) {
				count++;
			}
		for (int i = 0; i < Player.stack3.size(); i++)
			if (Player.stack3.get(i).cardType.equals("Resource")) {
				count++;
			}
		for (int i = 0; i < Player.stack4.size(); i++)
			if (Player.stack4.get(i).cardType.equals("Resource")) {
				count++;
			}
		for (int i = 0; i < Player.stack5.size(); i++)
			if (Player.stack5.get(i).cardType.equals("Resource")) {
				count++;
			}
		return count;

	}
	public static boolean fulldeck() {
		int count = 0;
		for (int i = 0; i < Player.stack1.size(); i++)
			if (Player.stack1.get(i).cardID != 0) {
				count++;
			}
		for (int i = 0; i < Player.stack2.size(); i++)
			if (Player.stack2.get(i).cardID != 0) {
				count++;
			}
		for (int i = 0; i < Player.stack3.size(); i++)
			if (Player.stack3.get(i).cardID != 0) {
				count++;
			}
		for (int i = 0; i < Player.stack4.size(); i++)
			if (Player.stack4.get(i).cardID != 0) {
				count++;
			}
		for (int i = 0; i < Player.stack5.size(); i++)
			if (Player.stack5.get(i).cardID != 0) {
				count++;
			}
		if (count == 15)
			return true;
		else
			return false;

	}
	public static int findposition(CurrentCard card) {
		int count = 0;

		if (card.cardType.equals("Character")) {
			if (Game.numresources() < Integer.parseInt(card.cost))
				return -1;

		}
		if (card.cardType.equals("Resource")) {
			if (Game.countallresources() == 3)
				return -2;

		}
		if (fulldeck())
			return -3;
		if (card.cardType.equals("Resource"))
			Player.canplayresource = false;
		boolean putinstack = false;
		for (int k = 0; k < 3; k++) {
			switch (k) {
			case 0:

				if (Player.stack1.get(0).cardID == 0
						&& card.cardType.equals("Resource")) {
					putinstack = true;
					Player.stack1.set(0, card);
					return R.id.mystack1card1;
				}
				if (Player.stack2.get(0).cardID == 0
						&& !card.cardType.equals("Resource")) {
					putinstack = true;
					Player.stack2.set(0, card);
					return R.id.mystack2card1;
				}
				if (Player.stack3.get(0).cardID == 0
						&& !card.cardType.equals("Resource")) {
					putinstack = true;
					Player.stack3.set(0, card);
					return R.id.mystack3card1;
				}
				if (Player.stack4.get(0).cardID == 0
						&& !card.cardType.equals("Resource")) {
					putinstack = true;
					Player.stack4.set(0, card);
					return R.id.mystack4card1;
				}
				if (Player.stack5.get(0).cardID == 0
						&& !card.cardType.equals("Resource")) {
					putinstack = true;
					Player.stack5.set(0, card);
					return R.id.mystack5card1;
				}
			case 1:
				if (Player.stack1.get(1).cardID == 0
						&& card.cardType.equals("Resource")) {
					putinstack = true;
					Player.stack1.set(1, card);
					return R.id.mystack1card2;
				}
				if (Player.stack2.get(1).cardID == 0
						&& card.type1.equals(Player.stack2.get(0).type1)
						&& !card.cardType.equals("Resource")) {
					putinstack = true;
					Player.stack2.set(1, card);
					return R.id.mystack2card2;
				}
				if (Player.stack3.get(1).cardID == 0
						&& card.type1.equals(Player.stack3.get(0).type1)
						&& !card.cardType.equals("Resource")) {
					putinstack = true;
					Player.stack3.set(1, card);
					return R.id.mystack3card2;
				}
				if (Player.stack4.get(1).cardID == 0
						&& card.type1.equals(Player.stack4.get(0).type1)
						&& !card.cardType.equals("Resource")) {
					putinstack = true;
					Player.stack4.set(1, card);
					return R.id.mystack4card2;
				}
				if (Player.stack5.get(1).cardID == 0
						&& card.type1.equals(Player.stack5.get(0).type1)
						&& !card.cardType.equals("Resource")) {
					putinstack = true;
					Player.stack5.set(1, card);
					return R.id.mystack5card2;
				}
			case 2:
				if (Player.stack1.get(2).cardID == 0
						&& card.cardType.equals("Resource")) {
					putinstack = true;
					Player.stack1.set(2, card);
					return R.id.mystack1card3;
				}
				if (Player.stack2.get(2).cardID == 0
						&& card.type1.equals(Player.stack2.get(1).type1)
						&& !card.cardType.equals("Resource")) {
					putinstack = true;
					Player.stack2.set(2, card);
					return R.id.mystack2card3;
				}
				if (Player.stack3.get(2).cardID == 0
						&& card.type1.equals(Player.stack3.get(1).type1)
						&& !card.cardType.equals("Resource")) {
					putinstack = true;
					Player.stack3.set(2, card);
					return R.id.mystack3card3;
				}
				if (Player.stack4.get(2).cardID == 0
						&& card.type1.equals(Player.stack4.get(1).type1)
						&& !card.cardType.equals("Resource")) {
					putinstack = true;
					Player.stack4.set(2, card);
					return R.id.mystack4card3;
				}
				if (Player.stack5.get(2).cardID == 0
						&& card.type1.equals(Player.stack5.get(1).type1)
						&& !card.cardType.equals("Resource")) {
					putinstack = true;
					Player.stack5.set(2, card);
					return R.id.mystack5card3;
				}
			}
			if (!putinstack) {
				switch (k) {
				case 0:

					if (Player.stack1.get(0).cardID == 0
							&& card.cardType.equals("Resource")) {
						Player.stack1.set(0, card);
						return R.id.mystack1card1;
					}
					if (Player.stack2.get(0).cardID == 0) {
						Player.stack2.set(0, card);
						return R.id.mystack2card1;
					}
					if (Player.stack3.get(0).cardID == 0) {
						Player.stack3.set(0, card);
						return R.id.mystack3card1;
					}
					if (Player.stack4.get(0).cardID == 0) {
						Player.stack4.set(0, card);
						return R.id.mystack4card1;
					}
					if (Player.stack5.get(0).cardID == 0) {
						Player.stack5.set(0, card);
						return R.id.mystack5card1;
					}
				case 1:
					if (Player.stack1.get(1).cardID == 0
							&& card.cardType.equals("Resource")) {
						Player.stack1.set(1, card);
						return R.id.mystack1card2;
					}
					if (Player.stack2.get(1).cardID == 0) {
						Player.stack2.set(1, card);
						return R.id.mystack2card2;
					}
					if (Player.stack3.get(1).cardID == 0) {
						Player.stack3.set(1, card);
						return R.id.mystack3card2;
					}
					if (Player.stack4.get(1).cardID == 0) {
						Player.stack4.set(1, card);
						return R.id.mystack4card2;
					}
					if (Player.stack5.get(1).cardID == 0) {
						Player.stack5.set(1, card);
						return R.id.mystack5card2;
					}
				case 2:
					if (Player.stack1.get(2).cardID == 0
							&& card.cardType.equals("Resource")) {
						Player.stack1.set(2, card);
						return R.id.mystack1card3;
					}
					if (Player.stack2.get(2).cardID == 0) {
						Player.stack2.set(2, card);
						return R.id.mystack2card3;
					}
					if (Player.stack3.get(2).cardID == 0) {
						Player.stack3.set(2, card);
						return R.id.mystack3card3;
					}
					if (Player.stack4.get(2).cardID == 0) {
						Player.stack4.set(2, card);
						return R.id.mystack4card3;
					}
					if (Player.stack5.get(2).cardID == 0) {
						Player.stack5.set(2, card);
						return R.id.mystack5card3;
					}
				}
			}
		}

		return 0;

	}

	public static List<String> parsecards(String s) {

		List<String> items = new LinkedList<String>(Arrays.asList(s
				.split("\\s*,\\s*")));
		return items;
	}

	public static double getmypowerlevel() {
		BigRational powerlevel = BigRational.valueOf("0");
		for (int i = 0; i < Player.stack1.size(); i++)
			if (!Player.stack1.get(i).tapped)
				powerlevel = powerlevel.add(Player.stack1.get(i).getPower());
		for (int i = 0; i < Player.stack2.size(); i++)
			if (!Player.stack2.get(i).tapped)
				powerlevel = powerlevel.add(Player.stack2.get(i).getPower());
		for (int i = 0; i < Player.stack3.size(); i++)
			if (!Player.stack3.get(i).tapped)
				powerlevel = powerlevel.add(Player.stack3.get(i).getPower());
		for (int i = 0; i < Player.stack4.size(); i++)
			if (!Player.stack4.get(i).tapped)
				powerlevel = powerlevel.add(Player.stack4.get(i).getPower());
		for (int i = 0; i < Player.stack5.size(); i++)
			if (!Player.stack5.get(i).tapped)
				powerlevel = powerlevel.add(Player.stack5.get(i).getPower());
		return powerlevel.doubleValue();
	}

	public static String getmypowerlevelstring() {
		BigRational powerlevel = BigRational.valueOf("0");
		for (int i = 0; i < Player.stack1.size(); i++)
			if (!Player.stack1.get(i).tapped)
				powerlevel = powerlevel.add(Player.stack1.get(i).getPower());
		for (int i = 0; i < Player.stack2.size(); i++)
			if (!Player.stack2.get(i).tapped)
				powerlevel = powerlevel.add(Player.stack2.get(i).getPower());
		for (int i = 0; i < Player.stack3.size(); i++)
			if (!Player.stack3.get(i).tapped)
				powerlevel = powerlevel.add(Player.stack3.get(i).getPower());
		for (int i = 0; i < Player.stack4.size(); i++)
			if (!Player.stack4.get(i).tapped)
				powerlevel = powerlevel.add(Player.stack4.get(i).getPower());
		for (int i = 0; i < Player.stack5.size(); i++)
			if (!Player.stack5.get(i).tapped)
				powerlevel = powerlevel.add(Player.stack5.get(i).getPower());
		String s = powerlevel.toString();
		s = s.replaceAll("\\s", "");
		return s;
	}
	public static String getbeforechallengestring() {
		String s = Player.beforechallenge.toString();
		s = s.replaceAll("\\s", "");
		return s;
	}
	public static String getmyhealthstring() {
		String s = Player.health.toString();
		s = s.replaceAll("\\s", "");
		return s;
	}

	public static BigRational getmypowerlevelfraction() {
		BigRational powerlevel = BigRational.valueOf("0");
		for (int i = 0; i < Player.stack1.size(); i++)
			if (!Player.stack1.get(i).tapped)
				powerlevel = powerlevel.add(Player.stack1.get(i).getPower());
		for (int i = 0; i < Player.stack2.size(); i++)
			if (!Player.stack2.get(i).tapped)
				powerlevel = powerlevel.add(Player.stack2.get(i).getPower());
		for (int i = 0; i < Player.stack3.size(); i++)
			if (!Player.stack3.get(i).tapped)
				powerlevel = powerlevel.add(Player.stack3.get(i).getPower());
		for (int i = 0; i < Player.stack4.size(); i++)
			if (!Player.stack4.get(i).tapped)
				powerlevel = powerlevel.add(Player.stack4.get(i).getPower());
		for (int i = 0; i < Player.stack5.size(); i++)
			if (!Player.stack5.get(i).tapped)
				powerlevel = powerlevel.add(Player.stack5.get(i).getPower());
		return powerlevel;
	}

	public static String toFraction(double d, int factor) {
		StringBuilder sb = new StringBuilder();
		if (d < 0) {
			sb.append('-');
			d = -d;
		}

		long l = (long) d;
		if (l != 0)
			sb.append(l);
		d -= l;

		double error = Math.abs(d);
		int bestDenominator = 1;
		for (int i = 2; i <= factor; i++) {
			double error2 = Math.abs(d - (double) Math.round(d * i) / i);
			if (error2 < error) {
				error = error2;
				bestDenominator = i;
			}
		}
		if (bestDenominator > 1)
			sb.append(' ').append(Math.round(d * bestDenominator)).append('/')
					.append(bestDenominator);
		return sb.toString();
	}

	public static String toFractionURL(double d, int factor) {
		StringBuilder sb = new StringBuilder();
		if (d < 0) {
			sb.append('-');
			d = -d;
		}

		long l = (long) d;
		if (l != 0)
			sb.append(l);
		d -= l;

		double error = Math.abs(d);
		int bestDenominator = 1;
		for (int i = 2; i <= factor; i++) {
			double error2 = Math.abs(d - (double) Math.round(d * i) / i);
			if (error2 < error) {
				error = error2;
				bestDenominator = i;
			}
		}
		if (bestDenominator > 1)
			sb.append('%').append('2').append('0')
					.append(Math.round(d * bestDenominator)).append('/')
					.append(bestDenominator);
		return sb.toString();
	}
	public static void updateboard2() throws JSONException {
		RestClient Connect = new RestClient();
		String mind1, charm1, strength1, cardname;
		List<String> type;
		List<String> level;
		String type1 = "";
		String type2 = "";
		String type3 = "";
		String levelup1 = "";
		String leveup2 = "";
		String levelup3 = "";
		String cost, stringtype, imgUri;
		boolean tapped;
		int cardID;
		Tuple attr, types, levelups, mind, charm, strength;
		String p = OpponentPlayer.getPlayer();
		CurrentCard card = new CurrentCard(0);
		for (int i = 0; i < OpponentPlayer.stack1.size(); i++)
			OpponentPlayer.stack1.set(i, card);

		String getstackcard1 = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
				+ Game.getGameID() + "&" + p + "stack1";
		Connect.connect(getstackcard1);
		int checkconnect = Connect.findindex(p + "stack1"); // read opponent
															// stacks
		List<String> stack1 = new ArrayList<String>();

		if (checkconnect != -1)
			stack1 = Game.parsecards(Connect.getvalArray().getString(
					checkconnect));

		if (checkconnect != -1 && !stack1.get(0).equals("null")) {
			for (int i = 0; i < stack1.size(); i++) {
				ArrayList<String> numbers = new ArrayList<String>(); // numbers
																		// from
																		// card

				Pattern pp = Pattern.compile("\\d+"); // put numbers into an
														// array that has one
														// with card id and
														// level
				Matcher m = pp.matcher(stack1.get(i));
				while (m.find()) {
					numbers.add(m.group());
				}

				cardID = Integer.parseInt(numbers.get(0));
				attr = new Tuple(CardLibrary.cardlibrary.get(cardID).getMind()
						.toString(), CardLibrary.cardlibrary.get(cardID)
						.getCharm().toString(), CardLibrary.cardlibrary
						.get(cardID).getStrength().toString());
				types = new Tuple(CardLibrary.cardlibrary.get(cardID).type1,
						CardLibrary.cardlibrary.get(cardID).type2,
						CardLibrary.cardlibrary.get(cardID).type3);
				cost = CardLibrary.cardlibrary.get(cardID).cost;
				levelups = new Tuple(CardLibrary.cardlibrary.get(cardID)
						.getLevelUp(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp3());
				mind = new Tuple(
						CardLibrary.cardlibrary.get(cardID).mind1.toString(),
						CardLibrary.cardlibrary.get(cardID).mind2.toString(),
						CardLibrary.cardlibrary.get(cardID).mind3.toString());
				strength = new Tuple(
						CardLibrary.cardlibrary.get(cardID).strength1
								.toString(),
						CardLibrary.cardlibrary.get(cardID).strength2
								.toString(), CardLibrary.cardlibrary
								.get(cardID).strength3.toString());
				charm = new Tuple(
						CardLibrary.cardlibrary.get(cardID).charm1.toString(),
						CardLibrary.cardlibrary.get(cardID).charm2.toString(),
						CardLibrary.cardlibrary.get(cardID).charm3.toString());
				cardname = CardLibrary.cardlibrary.get(cardID).name;
				if (stack1.get(i).contains("t"))
					tapped = true;
				else
					tapped = false;

				CurrentCard CurrentCard = new CurrentCard(cardID, attr, types,
						cost, levelups, mind, charm, strength, cardname);
				CurrentCard.tapped = tapped;
				CurrentCard.setlevel(numbers.get(1));
				OpponentPlayer.stack1.set(i, CurrentCard);

			}
		}
		Connect.clear();

		for (int i = 0; i < OpponentPlayer.stack2.size(); i++)
			OpponentPlayer.stack2.set(i, card);
		String getstackcard2 = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
				+ Game.getGameID() + "&" + p + "stack2";
		Connect.connect(getstackcard2);
		checkconnect = Connect.findindex(p + "stack2");
		List<String> stack2 = new ArrayList<String>();

		if (checkconnect != -1)
			stack2 = Game.parsecards(Connect.getvalArray().getString(
					checkconnect));
		if (checkconnect != -1 && !stack2.get(0).equals("null")) {
			for (int i = 0; i < stack2.size(); i++) {
				ArrayList<String> numbers = new ArrayList<String>(); // numbers
																		// from
																		// card

				Pattern pp = Pattern.compile("\\d+"); // put numbers into an
														// array that has one
														// with card id and
														// level
				Matcher m = pp.matcher(stack2.get(i));
				while (m.find()) {
					numbers.add(m.group());
				}
				cardID = Integer.parseInt(numbers.get(0));
				attr = new Tuple(CardLibrary.cardlibrary.get(cardID).getMind()
						.toString(), CardLibrary.cardlibrary.get(cardID)
						.getCharm().toString(), CardLibrary.cardlibrary
						.get(cardID).getStrength().toString());
				types = new Tuple(CardLibrary.cardlibrary.get(cardID).type1,
						CardLibrary.cardlibrary.get(cardID).type2,
						CardLibrary.cardlibrary.get(cardID).type3);
				cost = CardLibrary.cardlibrary.get(cardID).cost;
				levelups = new Tuple(CardLibrary.cardlibrary.get(cardID)
						.getLevelUp1(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2());
				mind = new Tuple(
						CardLibrary.cardlibrary.get(cardID).mind1.toString(),
						CardLibrary.cardlibrary.get(cardID).mind2.toString(),
						CardLibrary.cardlibrary.get(cardID).mind3.toString());
				strength = new Tuple(
						CardLibrary.cardlibrary.get(cardID).strength1
								.toString(),
						CardLibrary.cardlibrary.get(cardID).strength2
								.toString(), CardLibrary.cardlibrary
								.get(cardID).strength3.toString());
				charm = new Tuple(
						CardLibrary.cardlibrary.get(cardID).charm1.toString(),
						CardLibrary.cardlibrary.get(cardID).charm2.toString(),
						CardLibrary.cardlibrary.get(cardID).charm3.toString());
				cardname = CardLibrary.cardlibrary.get(cardID).name;
				if (stack2.get(i).contains("t"))
					tapped = true;
				else
					tapped = false;

				CurrentCard CurrentCard = new CurrentCard(cardID, attr, types,
						cost, levelups, mind, charm, strength, cardname);
				CurrentCard.tapped = tapped;
				CurrentCard.setlevel(numbers.get(1));
				OpponentPlayer.stack2.set(i, CurrentCard);

			}
		}
		Connect.clear();
		for (int i = 0; i < OpponentPlayer.stack3.size(); i++)
			OpponentPlayer.stack3.set(i, card);
		String getstackcard3 = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
				+ Game.getGameID() + "&" + p + "stack3";
		Connect.connect(getstackcard3);
		checkconnect = Connect.findindex(p + "stack3");
		List<String> stack3 = new ArrayList<String>();

		if (checkconnect != -1)
			stack3 = Game.parsecards(Connect.getvalArray().getString(
					checkconnect));
		if (checkconnect != -1 && !stack3.get(0).equals("null")) {
			for (int i = 0; i < stack3.size(); i++) {
				ArrayList<String> numbers = new ArrayList<String>(); // numbers
																		// from
																		// card

				Pattern pp = Pattern.compile("\\d+"); // put numbers into an
														// array that has one
														// with card id and
														// level
				Matcher m = pp.matcher(stack3.get(i));
				while (m.find()) {
					numbers.add(m.group());
				}
				cardID = Integer.parseInt(numbers.get(0));
				attr = new Tuple(CardLibrary.cardlibrary.get(cardID).getMind()
						.toString(), CardLibrary.cardlibrary.get(cardID)
						.getCharm().toString(), CardLibrary.cardlibrary
						.get(cardID).getStrength().toString());
				types = new Tuple(CardLibrary.cardlibrary.get(cardID).type1,
						CardLibrary.cardlibrary.get(cardID).type2,
						CardLibrary.cardlibrary.get(cardID).type3);
				cost = CardLibrary.cardlibrary.get(cardID).cost;
				levelups = new Tuple(CardLibrary.cardlibrary.get(cardID)
						.getLevelUp1(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2());
				mind = new Tuple(
						CardLibrary.cardlibrary.get(cardID).mind1.toString(),
						CardLibrary.cardlibrary.get(cardID).mind2.toString(),
						CardLibrary.cardlibrary.get(cardID).mind3.toString());
				strength = new Tuple(
						CardLibrary.cardlibrary.get(cardID).strength1
								.toString(),
						CardLibrary.cardlibrary.get(cardID).strength2
								.toString(), CardLibrary.cardlibrary
								.get(cardID).strength3.toString());
				charm = new Tuple(
						CardLibrary.cardlibrary.get(cardID).charm1.toString(),
						CardLibrary.cardlibrary.get(cardID).charm2.toString(),
						CardLibrary.cardlibrary.get(cardID).charm3.toString());
				cardname = CardLibrary.cardlibrary.get(cardID).name;
				if (stack3.get(i).contains("t"))
					tapped = true;
				else
					tapped = false;

				CurrentCard CurrentCard = new CurrentCard(cardID, attr, types,
						cost, levelups, mind, charm, strength, cardname);
				CurrentCard.tapped = tapped;
				CurrentCard.setlevel(numbers.get(1));
				OpponentPlayer.stack3.set(i, CurrentCard);

			}
		}
		Connect.clear();
		for (int i = 0; i < OpponentPlayer.stack4.size(); i++)
			OpponentPlayer.stack4.set(i, card);
		String getstackcard4 = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
				+ Game.getGameID() + "&" + p + "stack4";
		Connect.connect(getstackcard4);
		checkconnect = Connect.findindex(p + "stack4");
		List<String> stack4 = new ArrayList<String>();

		if (checkconnect != -1)
			stack4 = Game.parsecards(Connect.getvalArray().getString(
					checkconnect));
		if (checkconnect != -1 && !stack4.get(0).equals("null")) {
			for (int i = 0; i < stack4.size(); i++) {
				ArrayList<String> numbers = new ArrayList<String>(); // numbers
																		// from
																		// card

				Pattern pp = Pattern.compile("\\d+"); // put numbers into an
														// array that has one
														// with card id and
														// level
				Matcher m = pp.matcher(stack4.get(i));
				while (m.find()) {
					numbers.add(m.group());
				}
				cardID = Integer.parseInt(numbers.get(0));
				attr = new Tuple(CardLibrary.cardlibrary.get(cardID).getMind()
						.toString(), CardLibrary.cardlibrary.get(cardID)
						.getCharm().toString(), CardLibrary.cardlibrary
						.get(cardID).getStrength().toString());
				types = new Tuple(CardLibrary.cardlibrary.get(cardID).type1,
						CardLibrary.cardlibrary.get(cardID).type2,
						CardLibrary.cardlibrary.get(cardID).type3);
				cost = CardLibrary.cardlibrary.get(cardID).cost;
				levelups = new Tuple(CardLibrary.cardlibrary.get(cardID)
						.getLevelUp1(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2());
				mind = new Tuple(
						CardLibrary.cardlibrary.get(cardID).mind1.toString(),
						CardLibrary.cardlibrary.get(cardID).mind2.toString(),
						CardLibrary.cardlibrary.get(cardID).mind3.toString());
				strength = new Tuple(
						CardLibrary.cardlibrary.get(cardID).strength1
								.toString(),
						CardLibrary.cardlibrary.get(cardID).strength2
								.toString(), CardLibrary.cardlibrary
								.get(cardID).strength3.toString());
				charm = new Tuple(
						CardLibrary.cardlibrary.get(cardID).charm1.toString(),
						CardLibrary.cardlibrary.get(cardID).charm2.toString(),
						CardLibrary.cardlibrary.get(cardID).charm3.toString());
				cardname = CardLibrary.cardlibrary.get(cardID).name;
				if (stack4.get(i).contains("t"))
					tapped = true;
				else
					tapped = false;

				CurrentCard CurrentCard = new CurrentCard(cardID, attr, types,
						cost, levelups, mind, charm, strength, cardname);
				CurrentCard.tapped = tapped;
				CurrentCard.setlevel(numbers.get(1));
				OpponentPlayer.stack4.set(i, CurrentCard);

			}
		}
		Connect.clear();
		for (int i = 0; i < OpponentPlayer.stack5.size(); i++)
			OpponentPlayer.stack5.set(i, card);
		String getstackcard5 = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
				+ Game.getGameID() + "&" + p + "stack5";
		Connect.connect(getstackcard5);
		checkconnect = Connect.findindex(p + "stack5");
		List<String> stack5 = new ArrayList<String>();

		if (checkconnect != -1)
			stack5 = Game.parsecards(Connect.getvalArray().getString(
					checkconnect));
		if (checkconnect != -1 && !stack5.get(0).equals("null")) {
			for (int i = 0; i < stack5.size(); i++) {
				ArrayList<String> numbers = new ArrayList<String>(); // numbers
																		// from
																		// card

				Pattern pp = Pattern.compile("\\d+"); // put numbers into an
														// array that has one
														// with card id and
														// level
				Matcher m = pp.matcher(stack5.get(i));
				while (m.find()) {
					numbers.add(m.group());
				}
				cardID = Integer.parseInt(numbers.get(0));
				attr = new Tuple(CardLibrary.cardlibrary.get(cardID).getMind()
						.toString(), CardLibrary.cardlibrary.get(cardID)
						.getCharm().toString(), CardLibrary.cardlibrary
						.get(cardID).getStrength().toString());
				types = new Tuple(CardLibrary.cardlibrary.get(cardID).type1,
						CardLibrary.cardlibrary.get(cardID).type2,
						CardLibrary.cardlibrary.get(cardID).type3);
				cost = CardLibrary.cardlibrary.get(cardID).cost;
				levelups = new Tuple(CardLibrary.cardlibrary.get(cardID)
						.getLevelUp1(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2());
				mind = new Tuple(
						CardLibrary.cardlibrary.get(cardID).mind1.toString(),
						CardLibrary.cardlibrary.get(cardID).mind2.toString(),
						CardLibrary.cardlibrary.get(cardID).mind3.toString());
				strength = new Tuple(
						CardLibrary.cardlibrary.get(cardID).strength1
								.toString(),
						CardLibrary.cardlibrary.get(cardID).strength2
								.toString(), CardLibrary.cardlibrary
								.get(cardID).strength3.toString());
				charm = new Tuple(
						CardLibrary.cardlibrary.get(cardID).charm1.toString(),
						CardLibrary.cardlibrary.get(cardID).charm2.toString(),
						CardLibrary.cardlibrary.get(cardID).charm3.toString());
				cardname = CardLibrary.cardlibrary.get(cardID).name;
				if (stack5.get(i).contains("t"))
					tapped = true;
				else
					tapped = false;

				CurrentCard CurrentCard = new CurrentCard(cardID, attr, types,
						cost, levelups, mind, charm, strength, cardname);
				CurrentCard.tapped = tapped;

				CurrentCard.setlevel(numbers.get(1));
				OpponentPlayer.stack5.set(i, CurrentCard);

			}
		}
		Connect.clear();

	}
	public static void updateboard() throws JSONException {
		RestClient Connect = new RestClient();
		String mind1, charm1, strength1, cardname;
		List<String> type;
		List<String> level;
		String type1 = "";
		String type2 = "";
		String type3 = "";
		String levelup1 = "";
		String leveup2 = "";
		String levelup3 = "";
		String cost, stringtype, imgUri;
		boolean tapped;
		int cardID;
		Tuple attr, types, levelups, mind, charm, strength;
		String p = OpponentPlayer.getPlayer();
		CurrentCard card = new CurrentCard(0);
		for (int i = 0; i < OpponentPlayer.stack1.size(); i++)
			OpponentPlayer.stack1.set(i, card);

		String getstackcard1 = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
				+ Game.getGameID() + "&" + p + "stack1";
		Connect.connect(getstackcard1);
		int checkconnect = Connect.findindex(p + "stack1"); // read opponent
															// stacks
		List<String> stack1 = new ArrayList<String>();

		if (checkconnect != -1)
			stack1 = Game.parsecards(Connect.getvalArray().getString(
					checkconnect));

		if (checkconnect != -1 && !stack1.get(0).equals("null")) {
			for (int i = 0; i < stack1.size(); i++) {
				ArrayList<String> numbers = new ArrayList<String>(); // numbers
																		// from
																		// card

				Pattern pp = Pattern.compile("\\d+"); // put numbers into an
														// array that has one
														// with card id and
														// level
				Matcher m = pp.matcher(stack1.get(i));
				while (m.find()) {
					numbers.add(m.group());
				}

				cardID = Integer.parseInt(numbers.get(0));
				attr = new Tuple(CardLibrary.cardlibrary.get(cardID).getMind()
						.toString(), CardLibrary.cardlibrary.get(cardID)
						.getCharm().toString(), CardLibrary.cardlibrary
						.get(cardID).getStrength().toString());
				types = new Tuple(CardLibrary.cardlibrary.get(cardID).type1,
						CardLibrary.cardlibrary.get(cardID).type2,
						CardLibrary.cardlibrary.get(cardID).type3);
				cost = CardLibrary.cardlibrary.get(cardID).cost;
				levelups = new Tuple(CardLibrary.cardlibrary.get(cardID)
						.getLevelUp1(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2());
				mind = new Tuple(
						CardLibrary.cardlibrary.get(cardID).mind1.toString(),
						CardLibrary.cardlibrary.get(cardID).mind2.toString(),
						CardLibrary.cardlibrary.get(cardID).mind3.toString());
				strength = new Tuple(
						CardLibrary.cardlibrary.get(cardID).strength1
								.toString(),
						CardLibrary.cardlibrary.get(cardID).strength2
								.toString(), CardLibrary.cardlibrary
								.get(cardID).strength3.toString());
				charm = new Tuple(
						CardLibrary.cardlibrary.get(cardID).charm1.toString(),
						CardLibrary.cardlibrary.get(cardID).charm2.toString(),
						CardLibrary.cardlibrary.get(cardID).charm3.toString());
				cardname = CardLibrary.cardlibrary.get(cardID).name;
				if (stack1.get(i).contains("t"))
					tapped = true;
				else
					tapped = false;

				CurrentCard CurrentCard = new CurrentCard(cardID, attr, types,
						cost, levelups, mind, charm, strength, cardname);
				CurrentCard.tapped = tapped;
				CurrentCard.setlevel(numbers.get(1));
				OpponentPlayer.stack1.set(i, CurrentCard);

			}
		}
		Connect.clear();

		for (int i = 0; i < OpponentPlayer.stack2.size(); i++)
			OpponentPlayer.stack2.set(i, card);
		String getstackcard2 = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
				+ Game.getGameID() + "&" + p + "stack2";
		Connect.connect(getstackcard2);
		checkconnect = Connect.findindex(p + "stack2");
		List<String> stack2 = new ArrayList<String>();

		if (checkconnect != -1)
			stack2 = Game.parsecards(Connect.getvalArray().getString(
					checkconnect));
		if (checkconnect != -1 && !stack2.get(0).equals("null")) {
			for (int i = 0; i < stack2.size(); i++) {
				ArrayList<String> numbers = new ArrayList<String>(); // numbers
																		// from
																		// card

				Pattern pp = Pattern.compile("\\d+"); // put numbers into an
														// array that has one
														// with card id and
														// level
				Matcher m = pp.matcher(stack2.get(i));
				while (m.find()) {
					numbers.add(m.group());
				}
				cardID = Integer.parseInt(numbers.get(0));
				attr = new Tuple(CardLibrary.cardlibrary.get(cardID).getMind()
						.toString(), CardLibrary.cardlibrary.get(cardID)
						.getCharm().toString(), CardLibrary.cardlibrary
						.get(cardID).getStrength().toString());
				types = new Tuple(CardLibrary.cardlibrary.get(cardID).type1,
						CardLibrary.cardlibrary.get(cardID).type2,
						CardLibrary.cardlibrary.get(cardID).type3);
				cost = CardLibrary.cardlibrary.get(cardID).cost;
				levelups = new Tuple(CardLibrary.cardlibrary.get(cardID)
						.getLevelUp1(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2());
				mind = new Tuple(
						CardLibrary.cardlibrary.get(cardID).mind1.toString(),
						CardLibrary.cardlibrary.get(cardID).mind2.toString(),
						CardLibrary.cardlibrary.get(cardID).mind3.toString());
				strength = new Tuple(
						CardLibrary.cardlibrary.get(cardID).strength1
								.toString(),
						CardLibrary.cardlibrary.get(cardID).strength2
								.toString(), CardLibrary.cardlibrary
								.get(cardID).strength3.toString());
				charm = new Tuple(
						CardLibrary.cardlibrary.get(cardID).charm1.toString(),
						CardLibrary.cardlibrary.get(cardID).charm2.toString(),
						CardLibrary.cardlibrary.get(cardID).charm3.toString());
				cardname = CardLibrary.cardlibrary.get(cardID).name;
				if (stack2.get(i).contains("t"))
					tapped = true;
				else
					tapped = false;

				CurrentCard CurrentCard = new CurrentCard(cardID, attr, types,
						cost, levelups, mind, charm, strength, cardname);
				CurrentCard.tapped = tapped;
				CurrentCard.setlevel(numbers.get(1));
				OpponentPlayer.stack2.set(i, CurrentCard);

			}
		}
		Connect.clear();
		for (int i = 0; i < OpponentPlayer.stack3.size(); i++)
			OpponentPlayer.stack3.set(i, card);
		String getstackcard3 = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
				+ Game.getGameID() + "&" + p + "stack3";
		Connect.connect(getstackcard3);
		checkconnect = Connect.findindex(p + "stack3");
		List<String> stack3 = new ArrayList<String>();

		if (checkconnect != -1)
			stack3 = Game.parsecards(Connect.getvalArray().getString(
					checkconnect));
		if (checkconnect != -1 && !stack3.get(0).equals("null")) {
			for (int i = 0; i < stack3.size(); i++) {
				ArrayList<String> numbers = new ArrayList<String>(); // numbers
																		// from
																		// card

				Pattern pp = Pattern.compile("\\d+"); // put numbers into an
														// array that has one
														// with card id and
														// level
				Matcher m = pp.matcher(stack3.get(i));
				while (m.find()) {
					numbers.add(m.group());
				}
				cardID = Integer.parseInt(numbers.get(0));
				attr = new Tuple(CardLibrary.cardlibrary.get(cardID).getMind()
						.toString(), CardLibrary.cardlibrary.get(cardID)
						.getCharm().toString(), CardLibrary.cardlibrary
						.get(cardID).getStrength().toString());
				types = new Tuple(CardLibrary.cardlibrary.get(cardID).type1,
						CardLibrary.cardlibrary.get(cardID).type2,
						CardLibrary.cardlibrary.get(cardID).type3);
				cost = CardLibrary.cardlibrary.get(cardID).cost;
				levelups = new Tuple(CardLibrary.cardlibrary.get(cardID)
						.getLevelUp1(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2());
				mind = new Tuple(
						CardLibrary.cardlibrary.get(cardID).mind1.toString(),
						CardLibrary.cardlibrary.get(cardID).mind2.toString(),
						CardLibrary.cardlibrary.get(cardID).mind3.toString());
				strength = new Tuple(
						CardLibrary.cardlibrary.get(cardID).strength1
								.toString(),
						CardLibrary.cardlibrary.get(cardID).strength2
								.toString(), CardLibrary.cardlibrary
								.get(cardID).strength3.toString());
				charm = new Tuple(
						CardLibrary.cardlibrary.get(cardID).charm1.toString(),
						CardLibrary.cardlibrary.get(cardID).charm2.toString(),
						CardLibrary.cardlibrary.get(cardID).charm3.toString());
				cardname = CardLibrary.cardlibrary.get(cardID).name;
				if (stack3.get(i).contains("t"))
					tapped = true;
				else
					tapped = false;

				CurrentCard CurrentCard = new CurrentCard(cardID, attr, types,
						cost, levelups, mind, charm, strength, cardname);
				CurrentCard.tapped = tapped;
				CurrentCard.setlevel(numbers.get(1));
				OpponentPlayer.stack3.set(i, CurrentCard);

			}
		}
		Connect.clear();
		for (int i = 0; i < OpponentPlayer.stack4.size(); i++)
			OpponentPlayer.stack4.set(i, card);
		String getstackcard4 = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
				+ Game.getGameID() + "&" + p + "stack4";
		Connect.connect(getstackcard4);
		checkconnect = Connect.findindex(p + "stack4");
		List<String> stack4 = new ArrayList<String>();

		if (checkconnect != -1)
			stack4 = Game.parsecards(Connect.getvalArray().getString(
					checkconnect));
		if (checkconnect != -1 && !stack4.get(0).equals("null")) {
			for (int i = 0; i < stack4.size(); i++) {
				ArrayList<String> numbers = new ArrayList<String>(); // numbers
																		// from
																		// card

				Pattern pp = Pattern.compile("\\d+"); // put numbers into an
														// array that has one
														// with card id and
														// level
				Matcher m = pp.matcher(stack4.get(i));
				while (m.find()) {
					numbers.add(m.group());
				}
				cardID = Integer.parseInt(numbers.get(0));
				attr = new Tuple(CardLibrary.cardlibrary.get(cardID).getMind()
						.toString(), CardLibrary.cardlibrary.get(cardID)
						.getCharm().toString(), CardLibrary.cardlibrary
						.get(cardID).getStrength().toString());
				types = new Tuple(CardLibrary.cardlibrary.get(cardID).type1,
						CardLibrary.cardlibrary.get(cardID).type2,
						CardLibrary.cardlibrary.get(cardID).type3);
				cost = CardLibrary.cardlibrary.get(cardID).cost;
				levelups = new Tuple(CardLibrary.cardlibrary.get(cardID)
						.getLevelUp1(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2());
				mind = new Tuple(
						CardLibrary.cardlibrary.get(cardID).mind1.toString(),
						CardLibrary.cardlibrary.get(cardID).mind2.toString(),
						CardLibrary.cardlibrary.get(cardID).mind3.toString());
				strength = new Tuple(
						CardLibrary.cardlibrary.get(cardID).strength1
								.toString(),
						CardLibrary.cardlibrary.get(cardID).strength2
								.toString(), CardLibrary.cardlibrary
								.get(cardID).strength3.toString());
				charm = new Tuple(
						CardLibrary.cardlibrary.get(cardID).charm1.toString(),
						CardLibrary.cardlibrary.get(cardID).charm2.toString(),
						CardLibrary.cardlibrary.get(cardID).charm3.toString());
				cardname = CardLibrary.cardlibrary.get(cardID).name;
				if (stack4.get(i).contains("t"))
					tapped = true;
				else
					tapped = false;

				CurrentCard CurrentCard = new CurrentCard(cardID, attr, types,
						cost, levelups, mind, charm, strength, cardname);
				CurrentCard.tapped = tapped;
				CurrentCard.setlevel(numbers.get(1));
				OpponentPlayer.stack4.set(i, CurrentCard);

			}
		}
		Connect.clear();
		for (int i = 0; i < OpponentPlayer.stack5.size(); i++)
			OpponentPlayer.stack5.set(i, card);
		String getstackcard5 = "http://emath-project.appspot.com/emath/emathservicehttp?function=getgame&gameID="
				+ Game.getGameID() + "&" + p + "stack5";
		Connect.connect(getstackcard5);
		checkconnect = Connect.findindex(p + "stack5");
		List<String> stack5 = new ArrayList<String>();

		if (checkconnect != -1)
			stack5 = Game.parsecards(Connect.getvalArray().getString(
					checkconnect));
		if (checkconnect != -1 && !stack5.get(0).equals("null")) {
			for (int i = 0; i < stack5.size(); i++) {
				ArrayList<String> numbers = new ArrayList<String>(); // numbers
																		// from
																		// card

				Pattern pp = Pattern.compile("\\d+"); // put numbers into an
														// array that has one
														// with card id and
														// level
				Matcher m = pp.matcher(stack5.get(i));
				while (m.find()) {
					numbers.add(m.group());
				}
				cardID = Integer.parseInt(numbers.get(0));
				attr = new Tuple(CardLibrary.cardlibrary.get(cardID).getMind()
						.toString(), CardLibrary.cardlibrary.get(cardID)
						.getCharm().toString(), CardLibrary.cardlibrary
						.get(cardID).getStrength().toString());
				types = new Tuple(CardLibrary.cardlibrary.get(cardID).type1,
						CardLibrary.cardlibrary.get(cardID).type2,
						CardLibrary.cardlibrary.get(cardID).type3);
				cost = CardLibrary.cardlibrary.get(cardID).cost;
				levelups = new Tuple(CardLibrary.cardlibrary.get(cardID)
						.getLevelUp1(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2(), CardLibrary.cardlibrary.get(cardID)
						.getLevelUp2());
				mind = new Tuple(
						CardLibrary.cardlibrary.get(cardID).mind1.toString(),
						CardLibrary.cardlibrary.get(cardID).mind2.toString(),
						CardLibrary.cardlibrary.get(cardID).mind3.toString());
				strength = new Tuple(
						CardLibrary.cardlibrary.get(cardID).strength1
								.toString(),
						CardLibrary.cardlibrary.get(cardID).strength2
								.toString(), CardLibrary.cardlibrary
								.get(cardID).strength3.toString());
				charm = new Tuple(
						CardLibrary.cardlibrary.get(cardID).charm1.toString(),
						CardLibrary.cardlibrary.get(cardID).charm2.toString(),
						CardLibrary.cardlibrary.get(cardID).charm3.toString());
				cardname = CardLibrary.cardlibrary.get(cardID).name;
				if (stack5.get(i).contains("t"))
					tapped = true;
				else
					tapped = false;

				CurrentCard CurrentCard = new CurrentCard(cardID, attr, types,
						cost, levelups, mind, charm, strength, cardname);
				CurrentCard.tapped = tapped;

				CurrentCard.setlevel(numbers.get(1));
				OpponentPlayer.stack5.set(i, CurrentCard);

			}
		}
		Connect.clear();

	}
	public static String getGameID() {
		return GameID;
	}
	public static void setGameID(String gameID) {
		GameID = gameID;
	}
	public static boolean isCreatedgame() {
		return Createdgame;
	}
	public static void setCreatedgame(boolean createdgame) {
		Createdgame = createdgame;
	}

}
